package com.func.trigger.job;

import com.func.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import com.func.domain.activity.service.IRaffleActivitySkuStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * <p>
 * UpdateActivitySkuStockJob
 * </p>
 *
 * @author Simon
 * @version 2025.03.13.1.0.0
 * @description
 * @since 2025-03-13
 */

@Slf4j
@Component()
public class UpdateActivitySkuStockJob {
    @Resource
    protected IRaffleActivitySkuStockService skuStock;

    @Scheduled(cron = "0/5 * * * * ?")
    void exec(){
        try {
            log.info("定时任务, 更新活动sku库存 [延时队列获取, 降低数据库的更新频次, 不要产生竞争]");
            ActivitySkuStockKeyVO activitySkuStockKeyVO = skuStock.takeQueueValue();
            if (null == activitySkuStockKeyVO) return;
            log.info("定时任务,  更新活动sku库存 sku : {}, activityId : {}", activitySkuStockKeyVO.getSku(), activitySkuStockKeyVO.getActivityId());
            skuStock.updateActivitySkuStock(activitySkuStockKeyVO.getSku());
        } catch (InterruptedException e) {
            log.error("定时任务 : 更新活动sku库存失败", e);
        }
    }

}
